package com.charlie.meetingmanagement.service.impl;


import com.charlie.meetingmanagement.mapper.UserMapper;
import com.charlie.meetingmanagement.pojo.dto.UpdateRoleRequest;
import com.charlie.meetingmanagement.pojo.po.PageBean;
import com.charlie.meetingmanagement.pojo.po.Result;
import com.charlie.meetingmanagement.pojo.po.User;
import com.charlie.meetingmanagement.service.UserService;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    private UserMapper userMapper;


    @Override
    public List<User> select() {
        return userMapper.select();
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, Integer userId , String userName, Integer userAge, String userTel,
                         String userEmail, Integer userRoleA, Integer userRoleC, Integer userSuperAdmin, Integer userRoleB)
    {
        Long count = userMapper.count(userId,userName, userAge,userTel,userEmail,userRoleA,userRoleB,
                userRoleC,userSuperAdmin);
//        这里使用pageHelper进行分页，非常好用
        PageHelper.startPage(page, pageSize);
        List<User> userList = userMapper.page(userId,userName, userAge,userTel,userEmail,userRoleA,userRoleB,
                userRoleC,userSuperAdmin);

        return new PageBean(count,userList);
    }

    @Override
    public Result register(User user) {
        user.setUserCreateTime(LocalDateTime.now());
        user.setUserUpdateTime(LocalDateTime.now());
        try{
            userMapper.register(user);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("用户名或用户ID已存在");
        }
        return Result.success();
    }

    @Override
    public Result update(User user) {
        try {
            user.setUserUpdateTime(LocalDateTime.now());
            userMapper.update(user);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("更新失败");
        }
        return Result.success();
    }

    @Override
    public Result delete(Integer userId) {
        try {
            User user;
            user = userMapper.selectById(userId);
            if(user.getUserRoleA() == 1 || user.getUserRoleB() == 1 || user.getUserRoleC() == 1 || user.getUserSuperAdmin() == 1){
                throw new Exception();
            }
            userMapper.delete(userId);
        }catch (Exception e){
            log.info(e.getMessage());
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @Override
    public boolean adminLogin(Integer userId, String userPassword) {
        return userMapper.adminLogin(userId, userPassword);
    }

    @Override
    public Result banById(Integer userId, Integer isBanned) {
        User user = userMapper.selectById(userId);
        if (user.getUserRoleA() == 1 || user.getUserRoleB() == 1 ||
                user.getUserRoleC() == 1 || user.getUserSuperAdmin() == 1) {
            return Result.error("INVALID BAN");
        } else{
            userMapper.banById(userId,isBanned);
            return Result.success();
        }
    }

    @Override
    public Result getById(Integer userId) {
        User user = userMapper.getById(userId);
        return Result.success(user);
    }

    @Override
    public User getByUsername(String userName) {
        User user=userMapper.getByUsername(userName);
        return user;
    }

    @Override
    public Boolean updatePassword(Integer userId, String oldPassword, String newPassword) {
        Result result = getById(userId);
        User user = (User)result.getData();
        if (user.getUserPassword().equals(oldPassword)){
            user.setUserPassword(newPassword);
            userMapper.update(user);
            return true;
        }else
            return false;
    }

    @Override
    public Result updateRole(UpdateRoleRequest updateRoleRequest) {
        try {
            User me = userMapper.selectById(updateRoleRequest.getMyUserId());
            if (me.getUserRoleC() == 1 || me.getUserSuperAdmin() == 1) {
                if (updateRoleRequest.getUser().getUserSuperAdmin() == 1 && me.getUserSuperAdmin() != 1){
                    throw new Exception();
                }else {
                    userMapper.updateRole(updateRoleRequest.getUser());
                    return Result.success();
                }
            }
        } catch (Exception e) {
            log.info(e.getMessage());
            return Result.error("INVALID ROLE");
        }
        return Result.error("INVALID ROLE");
    }


}
